package com.example.mybatispdemo.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatispdemo.store.entity.SupplierEntity;
import com.example.mybatispdemo.store.entity.SupplierEntity;
import com.example.mybatispdemo.store.dao.SupplierDao;
import com.example.mybatispdemo.store.service.ISupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljp
 * @since 2022-10-31
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierDao, SupplierEntity> implements ISupplierService {

    @Override
    public boolean addSupplier(SupplierEntity supplier) {
        if(this.getById(supplier.getId())!=null){
            return false;
        }
        return this.save(supplier);
    }

    @Override
    public SupplierEntity getSupplierById(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<SupplierEntity> getSupplierByName(String name) {
        if(name==null){
            return null;
        }
        QueryWrapper<SupplierEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(SupplierEntity::getName,name);
        return this.list(queryWrapper);
    }



    @Override
    public boolean updateSupplierById(Integer id, SupplierEntity supplier) {
        SupplierEntity supplier1 = this.getById(id);
        if(supplier1==null){
            return false;
        }
        return this.updateById(supplier);
    }

    @Override
    public boolean deleteSupplierById(Integer id) {
        SupplierEntity supplier1 = this.getById(id);
        if(supplier1==null){
            return false;
        }
        QueryWrapper<SupplierEntity>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(SupplierEntity::getId,id);
        this.baseMapper.delete(queryWrapper);
        return true;
    }

    @Override
    public boolean deleteAll() {
        QueryWrapper<SupplierEntity>queryWrapper=new QueryWrapper<>();
        this.baseMapper.delete(queryWrapper);
        return true;
    }
}
